package com.xiaolin.service;

import com.xiaolin.dao.AddFriendsMapper;
import com.xiaolin.pojo.AddFriends;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author XiaoLin
 * @date 2019/2/26 11:18
 */
@Service
public class AddFriendsService {

    @Autowired
    private AddFriendsMapper addFriendsMapper;

    /**
     * 保存好友申请信息
     *
     * @param addFriends
     */
    public void insert(AddFriends addFriends) {
        addFriendsMapper.insert(addFriends);
    }

    /**
     * 判断是否正在申请
     *
     * @param userId
     * @param uid
     * @return
     */
    public boolean findIsRequest(String userId, String uid) {
        Example example = new Example(AddFriends.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("addUid", userId);
        criteria.andEqualTo("receiveUid", uid);
        criteria.andEqualTo("state", 1);
        List<AddFriends> friends = addFriendsMapper.selectByExample(example);
        if (friends.size() >= 1) {
            return true;
        }
        return false;

    }

    /**
     * 查询我的正在申请
     *
     * @param userId
     * @return
     */
    public List<AddFriends> findMyRequest(String userId) {
        Example example = new Example(AddFriends.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("addUid", userId);
        criteria.andEqualTo("state", 1);
        return addFriendsMapper.selectByExample(example);
    }

    /**
     * 获取被申请消息
     *
     * @param userId
     * @return
     */
    public List<AddFriends> findMyInfo(String userId) {
        return addFriendsMapper.findMyInfo(userId);
    }

    /**
     * 通过id查询申请信息
     *
     * @param addfId
     * @param userId
     * @return
     */
    public AddFriends findMyInfoById(Integer addfId, String userId) {
        Example e = new Example(AddFriends.class);
        Example.Criteria criteria = e.createCriteria();
        criteria.andEqualTo("addfId", addfId);
        criteria.andEqualTo("receiveUid", userId);
        return addFriendsMapper.selectOneByExample(e);
    }

    /**
     * 修改申请信息状态
     *
     * @param addfId
     * @param i
     */
    public void upState(Integer addfId, int i) {
        AddFriends addFriends = new AddFriends();
        addFriends.setState(i);
        addFriends.setAddfId(addfId);
        addFriendsMapper.updateByPrimaryKeySelective(addFriends);
    }

    /**
     * 删除好友请求信息
     *
     * @param addfId
     * @param userId
     * @return
     */
    public int delete(Integer addfId, String userId) {
        Example e = new Example(AddFriends.class);
        Example.Criteria criteria = e.createCriteria();
        criteria.andEqualTo("addfId", addfId);
        criteria.andEqualTo("receiveUid", userId);
        return addFriendsMapper.deleteByExample(e);
    }
}
